#include "Time.h"

#define     LED_PIN     GPIO_Pin_8

/**
* @brief TIM2 ISR
* @param
*    
*    
* @return none
*/
void TIM2_IRQHandler(void)
{
    if(usart3_cnt != 0)        //串口3接收
    {
        if(usart3_cnt!=pre_usart3_cnt)
        {
            pre_usart3_cnt = usart3_cnt;
            tim2_cnt3 = 0;
        }
        else
        {
            tim2_cnt3++;
            if(tim2_cnt3>=5)                //超时认为串口接收完成
            {
                tim2_cnt3=0;
                usart3_rx_ok=1;             //接收完成
                rec3_length = usart3_cnt;
                usart3_cnt=0;
                pre_usart3_cnt=0;
            }
        }
    }
    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);                      //清除中断标志位
}



/**
* @brief TIM2 ISR
* @param
*    
*    
* @return none
*/
void TIM4_IRQHandler(void)
{
    tim4_cnt1++;
    if(tim4_cnt1 >=50)
    {
        tim4_cnt1 = 0;
        GPIO_WriteBit(GPIOA, LED_PIN, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOA, LED_PIN)));
    }
    TIM_ClearITPendingBit(TIM4, TIM_IT_Update);      //清除中断标志位
}


/**
* @brief  TIM2 configuration function 1ms
* @param
*    
*    
* @return none
*/
void time2_config(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);            //使能TIM2时钟

    TIM_DeInit(TIM2);
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;                            //预分频器不分频
    TIM_TimeBaseInitStructure.TIM_Prescaler = 359;           //分频系数 72000 000/360=200KHZ
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;   //向上计数
    TIM_TimeBaseInitStructure.TIM_Period = 199;                                                    //预装值 1ms;            
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
    
    NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;                   //定时器2中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;       //抢占优先级4
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;               //子优先级0
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
    
    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);                        //清除中断标志位
    
    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);                       //允许定时器2更新中断
    TIM_Cmd(TIM2,ENABLE);                                           //使能定时器2
}




/**
* @brief  TIM4 configuration function 10ms
* @param
*    
*    
* @return none
*/
void time4_config(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);            //使能TIM4时钟

    TIM_DeInit(TIM4);
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;         //预分频器不分频
    TIM_TimeBaseInitStructure.TIM_Prescaler = 3599;                     //分频系数 72000 000/3600=20 000
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;     //向上计数
    TIM_TimeBaseInitStructure.TIM_Period = 199;                         //预装值     10ms
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
    
    NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;                   //定时器4中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;         //抢占优先级1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;                //子优先级0
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
    
    TIM_ClearITPendingBit(TIM4,TIM_IT_Update);                      //清除中断标志位
    
    TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);                        //允许定时器4更新中断
    TIM_Cmd(TIM4,ENABLE);                                           //关闭定时器4
}


/**
* @brief  TIM initialization
* @param
*    
*    
* @return none
*/
void time_init(void)
{
    time2_config();        //定时器2配置1ms   串口接收用
    time4_config();        //10ms
}

